package com.a0soft.gphone.aTruffleHog.di;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Sam Lu <ysl.sam.lu (at) gmail.com>
 *
 */

import java.io.File;

import android.os.Environment;
import android.os.StatFs;

import com.a0soft.gphone.aTruffleHog.util.CONSTS;
import com.a0soft.gphone.aTruffleHog.util.MiscUtil;
import com.a0soft.gphone.aTruffleHog.util.SDK11;
import com.a0soft.gphone.aTruffleHog.util.SDK9;

class StorageInfo extends BaseInfo {    
	//private static final String LOG_TAG = StorageInfo.class.getSimpleName();
	
	static void
    Build(DeviceInfo di)
    {
        _BuildInternalStorageInfo(di);
        _BuildExternalStorageInfo(di);
    }
	
	private static void
	_BuildInternalStorageInfo(DeviceInfo di)
	{
	    File path = Environment.getDataDirectory();
	    long blockSize, totalBlocks;
	    
	    try {
    	    StatFs stat = new StatFs(path.getPath());        
    	    blockSize = stat.getBlockSize();
    	    totalBlocks = stat.getBlockCount();
    
    	    di.Put(DeviceInfoKey.K.SG_INT_PATH, path.getPath());
    	    di.Put(DeviceInfoKey.K.SG_INT_SIZE, totalBlocks * blockSize);	    
	    } catch (Exception e) {
	        /*
            Caused by: java.lang.IllegalArgumentException
            at android.os.StatFs.native_setup(Native Method)
            at android.os.StatFs.<init>(StatFs.java:32)
            */
            di.Put(DeviceInfoKey.K.SG_INT_PATH, CONSTS.UNAVAILABLE);
            di.Put(DeviceInfoKey.K.SG_INT_SIZE, CONSTS.UNAVAILABLE); 
	    }
	}
	
	private static void
	_BuildExternalStorageInfo(DeviceInfo di)
    {
	    int nExtStorageNum = 1;
	    
	    StatFs stat;
        long blockSize, totalBlocks;
        File path = Environment.getExternalStorageDirectory();
        if (path.exists()) {  
            try {
                stat = new StatFs(path.getPath());        
                blockSize = stat.getBlockSize();
                totalBlocks = stat.getBlockCount();
        
                di.Put(DeviceInfoKey.K.SG_1ST_EXT_PATH, path.getPath());
                
                String strStatus = Environment.getExternalStorageState();
                if (strStatus.equals(Environment.MEDIA_MOUNTED)) {
                    di.Put(DeviceInfoKey.K.SG_1ST_EXT_SIZE, totalBlocks * blockSize); 
                }
                else {
                    di.Put(DeviceInfoKey.K.SG_1ST_EXT_SIZE, CONSTS.UNAVAILABLE); 
                }
                
                if (MiscUtil.GetSDKVersion() >= 9) {
                    di.Put(DeviceInfoKey.K.SG_1ST_EXT_REMOVABLE,
                           SDK9.GetInstance().Environment_isExternalStorageRemovable());
                }
                else {
                    di.Put(DeviceInfoKey.K.SG_1ST_EXT_REMOVABLE, CONSTS.UNAVAILABLE);
                }
                
                if (MiscUtil.GetSDKVersion() >= 11) {
                    di.Put(DeviceInfoKey.K.SG_1ST_EXT_EMULATED,
                           SDK11.GetInstance().Environment_isExternalStorageEmulated());
                }
                else {
                    di.Put(DeviceInfoKey.K.SG_1ST_EXT_EMULATED, CONSTS.UNAVAILABLE);
                }
            } catch (Exception e) {
                /*
                Caused by: java.lang.IllegalArgumentException
                at android.os.StatFs.native_setup(Native Method)
                at android.os.StatFs.<init>(StatFs.java:32)
                */
                di.Put(DeviceInfoKey.K.SG_1ST_EXT_PATH, CONSTS.UNAVAILABLE);
                di.Put(DeviceInfoKey.K.SG_1ST_EXT_SIZE, CONSTS.UNAVAILABLE); 
                di.Put(DeviceInfoKey.K.SG_1ST_EXT_REMOVABLE, CONSTS.UNAVAILABLE);
                di.Put(DeviceInfoKey.K.SG_1ST_EXT_EMULATED, CONSTS.UNAVAILABLE);
            }
        }
        else {
            //?? should not happen
            di.Put(DeviceInfoKey.K.SG_1ST_EXT_PATH, CONSTS.UNAVAILABLE);
            di.Put(DeviceInfoKey.K.SG_1ST_EXT_SIZE, CONSTS.UNAVAILABLE); 
            di.Put(DeviceInfoKey.K.SG_1ST_EXT_REMOVABLE, CONSTS.UNAVAILABLE);
            di.Put(DeviceInfoKey.K.SG_1ST_EXT_EMULATED, CONSTS.UNAVAILABLE);
        }
        
        String[] strPaths = new String[] {
            "/emmc", 
            "/Removable/MicroSD", 
            "/disk", 
            "/sdcard2", 
            "/sdcard/_ExternalSD", 
            "/sdcard/external_sd", 
            "/sdcard/sd", 
            "/mnt/extsd", //Ainol Novo 7 Aurora
            "/mnt/external_sd", 
            "/mnt/external1", 
            "/mnt/Removable/MicroSD", 
            "/mnt/sdcard-ext", 
            "/mnt/sdcard/sdcard1", //Infotmic device
            "/mnt/sdcard/ext_sd", //HTC Flyer
        };
        for (String strPath: strPaths) {
            path = new File(strPath);
            if (!path.exists()) continue;
            try {
                stat = new StatFs(strPath);
                blockSize = stat.getBlockSize();
                totalBlocks = stat.getBlockCount();
                
                di.Put(DeviceInfoKey.K.SG_2ND_EXT_PATH, path.getPath());
                di.Put(DeviceInfoKey.K.SG_2ND_EXT_SIZE, totalBlocks * blockSize);
                
                ++nExtStorageNum;
                break;
            } catch (Exception e) {
                /*
                Caused by: java.lang.IllegalArgumentException
                at android.os.StatFs.native_setup(Native Method)
                at android.os.StatFs.<init>(StatFs.java:32)
                at com.a0soft.gphone.aTruffleHog.b.o.c(StorageInfo.java:111)
                */
            }
        }
        
        if (nExtStorageNum == 1) {
            di.Put(DeviceInfoKey.K.SG_2ND_EXT_PATH, CONSTS.UNAVAILABLE);
            di.Put(DeviceInfoKey.K.SG_2ND_EXT_SIZE, CONSTS.UNAVAILABLE);
        }
        
        di.Put(DeviceInfoKey.K.SG_EXT_NUM, nExtStorageNum);
    }
}
